package com.hongdee.crm.task.service.impl;

import com.hongdee.annotation.MethodComment;
import com.hongdee.common.util.DateUtils;
import com.hongdee.crm.task.dao.IBcMaintainTypeTaskDao;
import com.hongdee.crm.task.dao.ITCrmTaskDao;
import com.hongdee.crm.task.service.ITCrmCarBackToFacTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import java.util.List;
import java.util.Map;

/**
 * 名  称: TCrmBackToFacTaskServiceImpl
 * 类  型: class
 * 描  述: 车辆回厂类相关定时任务
 * 路  径: com.hongdee.crm.task.service.impl
 * 作  者: wangshuai
 * 日  期:
 */
@Component
public class TCrmCarBackToFacTaskServiceImpl implements ITCrmCarBackToFacTaskService {

    @Autowired
    private IBcMaintainTypeTaskDao bcMaintainTypeTaskDao;

    @Autowired
    private ITCrmTaskDao crmTaskDao;

    //----------------------------保养回厂开始---------------------------------------------------------------------------

    /**
     * 保养回厂
     * 执行步骤：
     * 1、查询t_bc_param_set设置表，查询定义保养相关类型的公司
     * 2、遍历公司信息，生成相应sql
     * 3、根据sql、参数并执行
     */
    @Override
    @MethodComment(name = "更新保养/流失回厂任务")
    public Integer carMaintainBackToFacQuartz() throws Exception {
        Integer nums=0;
        List<Map<String,Object>> lists=bcMaintainTypeTaskDao.findCompanyByWorkOrder();
        //遍历公司信息
        for (Map<String,Object> map:lists) {
            //公司ID
            String companyId=String.valueOf(map.get("company_id"));
            //维修项目
            String[] itemNames=String.valueOf(map.get("itemName")).split(",");
            StringBuffer sqlStr=new StringBuffer();
            List<Object> params=new ArrayList<>();
            this.analyMaintainItemNameToSql(itemNames,sqlStr,params,companyId);
            int num=crmTaskDao.carBackToFacTask(sqlStr.toString(),params);
            nums+=num;
        }
        return nums;
    }



    /**
     * 车辆保养回厂，生成sql
     * @param itemNames 对应该公司所设置的维修项目
     * @param sqlStr
     * @param params
     */
    private void analyMaintainItemNameToSql(String[] itemNames,StringBuffer sqlStr,List<Object> params,String companyId){
        sqlStr.append("UPDATE t_crm_task_real t SET is_back = '1'  WHERE t.type_pcode in('by','ls') " +
                " and t.expect_date<>'日期选择'  and t.company_id=? " +
                " AND EXISTS ( SELECT * FROM t_crm_car_workorder w WHERE t.vin = w.vin " +
                " AND DATE_FORMAT(FROM_UNIXTIME(balance_Date/1000), '%Y%m') IN " +
                " ( DATE_FORMAT(t.expect_Date, '%Y%m'), DATE_FORMAT( DATE_ADD( t.expect_Date, INTERVAL 1 MONTH ), '%Y%m' )," +
                " DATE_FORMAT( DATE_SUB( t.expect_Date, INTERVAL 1 MONTH ), '%Y%m' ) ) " +
                " AND real_amount > '0.00' " +
                " AND wx_type in ('首保','二保','定保') " +
                " AND company_id=? " +
                " ) " +
                " AND (t.is_back=0 or t.is_back is null) ");
        params.add(companyId);
        params.add(companyId);
    }

    //----------------------------保养回厂结束---------------------------------------------------------------------------

    //----------------------------保养回厂关闭任务开始--------------------------------------------------------------------
    @Override
    @MethodComment(name = "更新保养/流失回厂任务，并关闭任务")
    public Integer closeMaintainBackToFacTaskQuartz() throws Exception {
        Integer nums=0;
        List<Map<String,Object>> lists=bcMaintainTypeTaskDao.findCompanyByWorkOrder();
        //遍历公司信息
        for (Map<String,Object> map:lists) {
            //公司ID
            String companyId=String.valueOf(map.get("company_id"));
            //维修项目
            StringBuffer sqlStr=new StringBuffer();
            List<Object> params=new ArrayList<>();
            this.closeMaintainTaskToSql(sqlStr,params,companyId);
            int num=crmTaskDao.carBackToFacTask(sqlStr.toString(),params);
            nums+=num;
        }
        return nums;
    }

    /**
     * 车辆保养回厂，生成sql
     * @param sqlStr
     * @param params
     */
    private void closeMaintainTaskToSql(StringBuffer sqlStr,List<Object> params,String companyId){
        sqlStr.append("UPDATE t_crm_task_real t SET is_back = '1',status='2',deal_date=DATE_FORMAT(NOW(), '%Y-%m-%d') " +
                " WHERE  t.type_pcode in('by','ls') and t.expect_date<>'日期选择' and t.company_id=? " +
                " AND EXISTS ( SELECT * FROM t_crm_car_workorder w WHERE t.vin = w.vin " +
                " AND DATE_FORMAT(FROM_UNIXTIME(balance_Date/1000), '%Y%m') IN " +
                " ( DATE_FORMAT(t.expect_Date, '%Y%m'), DATE_FORMAT( DATE_ADD( t.expect_Date, INTERVAL 1 MONTH ), '%Y%m' ),DATE_FORMAT( DATE_SUB( t.expect_Date, INTERVAL 1 MONTH ), '%Y%m' ) ) " +
                " AND real_amount > '0.00' " +
                " AND wx_type in ('首保','二保','定保') " +
                " AND company_id=?" +
                ") " +
//                "AND t.status=0 " +
                "");
        params.add(companyId);
        params.add(companyId);
    }
    //----------------------------保养回厂关闭任务开始--------------------------------------------------------------------

    //----------------------------流失回厂开始---------------------------------------------------------------------------

    /**
     * 车辆流失回厂
     * 1、检索除事故车以外的有效进厂，示为有效进厂，更新流失回厂
     * @return
     * @throws Exception
     */
    @Override
    @MethodComment(name = "更新流失回厂任务")
    public Integer carLsTaskBackToFacQuartz() throws Exception {
        Integer nums=0;
        List<Map<String,Object>> lists=bcMaintainTypeTaskDao.findCompanyByWorkOrder();
        //遍历公司信息
        for (Map<String,Object> map:lists) {
            //公司ID
            String companyId=String.valueOf(map.get("company_id"));
            StringBuffer sqlStr=new StringBuffer();
            List<Object> params=new ArrayList<>();
            this.analyLsTaskItemNameToSql(sqlStr,params,companyId);
            int num=crmTaskDao.carBackToFacTask(sqlStr.toString(),params);
            nums+=num;
        }
        return nums;
    }



    /**
     * 车辆保养回厂，生成sql
     * @param sqlStr
     * @param params
     */
    private void analyLsTaskItemNameToSql(StringBuffer sqlStr,List<Object> params,String companyId){
        sqlStr.append("UPDATE t_crm_task_real t SET is_back = '1'  WHERE t.type_pcode ='ls' and t.company_id=? " +
                "AND EXISTS ( SELECT * FROM t_crm_car_workorder w WHERE t.vin = w.vin " +
                "AND DATE_FORMAT(FROM_UNIXTIME(balance_Date/1000), '%Y%m') IN " +
                "( DATE_FORMAT(t.expect_Date, '%Y%m'), DATE_FORMAT( DATE_ADD( t.expect_Date, INTERVAL 1 MONTH ), '%Y%m' )," +
                "DATE_FORMAT( DATE_SUB( t.expect_Date, INTERVAL 1 MONTH ), '%Y%m' ) ) " +
                "AND real_amount > '0.00' " +
                "AND wx_type not in ('事故车') " +
                "AND company_id=? " +
                ") " +
                "AND (t.is_back=0 or t.is_back is null) ");
        params.add(companyId);
        params.add(companyId);
    }

    //----------------------------流失回厂结束---------------------------------------------------------------------------

    //----------------------------流失回厂关闭任务开始--------------------------------------------------------------------
    @Override
    @MethodComment(name = "更新保养/流失回厂任务，并关闭任务")
    public Integer closelsTaskBackToFacTaskQuartz() throws Exception {
        Integer nums=0;
        List<Map<String,Object>> lists=bcMaintainTypeTaskDao.findCompanyByWorkOrder();
        //遍历公司信息
        for (Map<String,Object> map:lists) {
            //公司ID
            String companyId=String.valueOf(map.get("company_id"));
            StringBuffer sqlStr=new StringBuffer();
            List<Object> params=new ArrayList<>();
            this.closeLsTaskToSql(sqlStr,params,companyId);
            int num=crmTaskDao.carBackToFacTask(sqlStr.toString(),params);
            nums+=num;
        }
        return nums;
    }

    /**
     * 车辆保养回厂，生成sql
     * @param sqlStr
     * @param params
     */
    private void closeLsTaskToSql(StringBuffer sqlStr,List<Object> params,String companyId){
        sqlStr.append("UPDATE t_crm_task_real t SET is_back = '1',status='2',deal_date=DATE_FORMAT(NOW(), '%Y-%m-%d') WHERE  t.type_pcode ='ls' and t.company_id=? " +
                "AND EXISTS ( SELECT * FROM t_crm_car_workorder w WHERE t.vin = w.vin " +
                "AND DATE_FORMAT(FROM_UNIXTIME(balance_Date/1000), '%Y%m') IN " +
                "( DATE_FORMAT(t.expect_Date, '%Y%m'), DATE_FORMAT( DATE_ADD( t.expect_Date, INTERVAL 1 MONTH ), '%Y%m' ),DATE_FORMAT( DATE_SUB( t.expect_Date, INTERVAL 1 MONTH ), '%Y%m' ) ) " +
                "AND real_amount > '0.00' " +
                "AND wx_type not in ('事故车') " +
                "AND company_id=?" +
                ") " +
//                "AND t.status=0 " +
                "");
        params.add(companyId);
        params.add(companyId);
    }

    //----------------------------流失回厂关闭任务结束--------------------------------------------------------------------
}
